Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C# / VB.NET - [VB.NET][Direct3D 9]Algoritmo per sparo (sì, ancora)
Forum - C# / VB.NET - [VB.NET][Direct3D 9]Algoritmo per sparo (sì, ancora)

Avatar
GN (Member)
Guru


Messaggi: 772
Iscritto: 30/04/2011

Segnala al moderatore
Postato alle 19:13
Venerdì, 18/11/2011
Come già segnalato in topic precedenti (http://www.pierotofy.it/pages/extras/forum/16/1032564-%5Bv ..., sto sviluppando un semplice gioco di carri armati in directx con vb.net. Da mesi non riesco a farli sparare; ora ho scritto un algoritmo più efficace di quello di prima, utilizzando un sistema di equazioni, ma non funziona. Posto il codice in cui rilevo lo sparo:
Codice sorgente - presumibilmente VB.NET

  1. Dim MinNemico As New Vector3()
  2.                     Dim MaxNemico As New Vector3()
  3.                     Me.BoundingBox(Nemico, MinNemico, MaxNemico)
  4.                     Dim Colpito As Boolean = False
  5.                     'equazione retta
  6.                     'y + giocatore.pos.Z = coeff * x + giocatore.pos.X
  7.                     'y + giocatore.pos.Z - coeff * x - giocatore.pos.X = 0
  8.                     Dim coeff As Double = Math.Tan(90 - Rad(AngoloGiocatore))
  9.                     Dim a1 As Double = -coeff
  10.                     Dim b1 As Double = 1
  11.                     Dim c1 As Double = -(Giocatore.pos.Z - Giocatore.pos.X)
  12.                     'equazione diagonale rettangolo
  13.                     '(y-y1)\(y2-y1)=(x-x1)\(x2-x1)
  14.                     '(y - minnemico.z)\(maxnemico.z - minnemico.z) = (x - minnemico.x)\(maxnemico.x - minnemico.x)
  15.                     Dim n1 As Double = MinNemico.Z
  16.                     Dim n2 As Double = MaxNemico.Z - MinNemico.Z
  17.                     Dim n3 As Double = MinNemico.X
  18.                     Dim n4 As Double = MaxNemico.X - MinNemico.X
  19.                     '(y - n1)\n2 = (x - n3)\n4
  20.                     'n4(y-n1) = n2(x-n3)
  21.                     'n4*y + n4*n1 = n2*x - n2*n3
  22.                     '-n2*x + n4*y = n2*n3 - n4*n3
  23.                     Dim a2 As Double = -n2
  24.                     Dim b2 As Double = n4
  25.                     Dim c2 As Double = n2 * n3 - n4 * n3
  26.                     Dim p As PointF = Me.Cramer(a1, b1, c1, a2, b2, c2)
  27.                     Dim rect As New RectangleF(MinNemico.X, MinNemico.Z, MaxNemico.X - MinNemico.X, MaxNemico.Z - MinNemico.Z)
  28.                     If rect.Contains(p) Then
  29.                         Nemici.RemoveAt(i)
  30.                         Sparo.Play(0, BufferPlayFlags.Default)
  31.                         If Nemici.Count = 0 Then
  32.                             My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\Livello.txt", Livello + 1, False)
  33.                             FineLivello.Play(0, BufferPlayFlags.Default)
  34.                             TimerTempo.Stop()
  35.                             Me.GeneraLivello()
  36.                         End If
  37.                         Exit For
  38.                     End If


E quello della funzione cramer, che dovrebbe restituire il punto di intersezione.
Codice sorgente - presumibilmente VB.NET

  1. Function Cramer(ByVal a1 As Double, ByVal b1 As Double, ByVal c1 As Double, ByVal a2 As Double, ByVal b2 As Double, ByVal c2 As Double) As PointF
  2.         'a1 b1
  3.         'a2 b2
  4.         Dim D As Double = a1 * b2 - b1 * a2
  5.         'c1 b1
  6.         'c2 b2
  7.         Dim Dx As Double = c1 * b2 - b1 * c2
  8.         'a1 c1
  9.         'a2 c2
  10.         Dim Dy As Double = a1 * c2 - c1 * a2
  11.         Return New PointF(Dx / D, Dy / D)
  12.     End Function


Per favore aiutatemi, è un sacco di tempo che ho questo problema :(:(:(:(... grazie in anticipo.

PM